package cn.itcast.controller;

import cn.itcast.domain.Account;
import cn.itcast.domain.GroupA;
import cn.itcast.domain.GroupB;
import cn.itcast.exception.BusinessException;
import cn.itcast.result.CodeConstant;
import cn.itcast.result.MessageConstant;
import cn.itcast.result.Result;
import cn.itcast.service.AccountService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @GetMapping("/find")
    public Object findAll()throws Exception{
        List<Account> list = accountService.findAll();
        return list;
    }

    @GetMapping("/findById/{id}")
    public Object findById(@PathVariable Integer id)throws Exception{
       return accountService.findById(id);
    }

    @PostMapping("/add")
    public Object add(@Validated(GroupA.class) @RequestBody Account account, Errors errors)throws Exception{
        HashMap<Object, Object> errorInfo = new HashMap<>();
        accountService.add(account);
        if (errors.hasErrors()){
            for (FieldError fieldError : errors.getFieldErrors()) {
                errorInfo.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
        }
        if (errorInfo.size()==0){
            return "success";
        }else {
            return errorInfo;
        }
    }

    @PutMapping("/update")
    public Object update(@Validated(GroupB.class)  Account account, Errors errors){
        HashMap<Object, Object> errorInfo = new HashMap<>();
        try {
            accountService.update(account);
            if (errors.hasErrors()){
                for (FieldError fieldError : errors.getFieldErrors()) {
                    errorInfo.put(fieldError.getField(),fieldError.getDefaultMessage());
                }
            }
            if (errorInfo.size()==0){
                return new Result(true, CodeConstant.SUCCESS,"success", MessageConstant.ACCOUNT_UPDATE_SUCCESS);
            }
            System.out.println(1/0);
            return errorInfo;
        } catch (Exception e) {
            e.printStackTrace();
            //return new Result(false,CodeConstant.ERROR,null,MessageConstant.ACCOUNT_UPDATE_ERROR);
            throw new BusinessException(CodeConstant.ERROR,MessageConstant.ACCOUNT_UPDATE_ERROR);
        }
    }

    @DeleteMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id)throws Exception{
        accountService.delete(id);
        return "success";
    }

    @GetMapping("/pageQuery/{pageNum}/{pageSize}")
    public Object pageQuery(@PathVariable Integer pageNum,@PathVariable Integer pageSize)throws Exception{
        PageInfo<Account> pageInfo = accountService.pageQuery(pageNum, pageSize);
        return pageInfo;
    }
}
